package recite.word.client.controller;

import cn.hutool.json.JSONObject;
import cn.hutool.json.JSONUtil;
import io.swagger.annotations.Api;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import recite.word.mybatis.auto.model.Word;
import recite.word.service.base.common.request.BaseRequest;
import recite.word.service.base.util.common.Asserts;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.util.ArrayList;
import java.util.List;

/**
 * @program: recite-word-back
 * @description:
 * @author: Mr.lin
 * @create: 2023-01-02 17:02
 **/

@RestController
@RequestMapping("/test")
@Api(tags = "客户端【测试-测试桩】Controller")
public class TestController {
	@PostMapping("/haha")
	public String haha(@RequestBody BaseRequest<String> input){
		String data = input.getData();
		Asserts.isNotBlank(data,"参数为空");
		return data;
	}

	public void test() throws Exception{
		String filePath = "C:\\Users\\linyuxian\\Desktop\\word\\考研词汇\\20";
		File parentFile = new File(filePath);
		File[] listFiles = parentFile.listFiles();
		List<Word> wordList = new ArrayList<>();
		if (null != listFiles) {
			for (File file : listFiles) {
				FileReader reader = new FileReader(file);
				BufferedReader br = new BufferedReader(reader);
				String s = br.readLine();
				int sStart = s.indexOf('{');
				while (sStart != -1 && s.charAt(sStart + 1) != '"'){
					System.out.println("43  " + s);
					sStart = s.indexOf('{',sStart + 1);
				}
				int sEnd = s.lastIndexOf('}');
				String json = "";
				try {
					json = s.substring(sStart, sEnd + 1);
				} catch (StringIndexOutOfBoundsException e){
					String s2 = br.readLine();
					int sStart2 = s2.indexOf('{');
					try{
						if (s2.charAt(sStart2 + 1) != '"'){
							sStart2 = s2.indexOf('{',sStart2 + 1);
						}
					} catch (Exception e2){
						System.out.println(file.getName());
					}

					int sEnd2 = s2.lastIndexOf('}');
					System.out.println("57  " + file.getName());
					json = s2.substring(sStart2,sEnd2 + 1);
					System.out.println("59  " + json);
				}
				JSONObject jsonObject = null;
				try {
					jsonObject = JSONUtil.parseObj(json);
				} catch (Exception e) {
					System.out.println(e.getMessage());
					System.out.println("65  " + file.getName());
					System.out.println("66  " + json);
				}
				Word word = Word.builder()
						.accent(jsonObject.getStr("accent"))
						.bookId(1610175510059708857L)
						.clozeData(jsonObject.getStr("cloze_data"))
						.deformationImg(jsonObject.getStr("deformation_img"))
						.imageFile(jsonObject.getStr("image_file"))
						.meanCn(jsonObject.getStr("mean_cn"))
						.meanEn(jsonObject.getStr("mean_en"))
						.sentence(jsonObject.getStr("sentence"))
						.sentenceAudio(jsonObject.getStr("sentence_audio"))
						.sentencePhrase(jsonObject.getStr("sentence_phrase"))
						.sentenceTrans(jsonObject.getStr("sentence_trans"))
						.tagId(jsonObject.getInt("tag_id"))
						.topicId(jsonObject.getInt("topic_id"))
						.word(jsonObject.getStr("word"))
						.wordAudio(jsonObject.getStr("word_audio"))
						.wordEtyma(jsonObject.getStr("word_etyma"))
						.wordLevelId(jsonObject.getInt("word_level_id"))
						.build();
				wordList.add(word);
				br.close();
				reader.close();
			}
		}
	}
}
